<?php

/* @version $Id: class.language.php,v 1.5 2008-02-16 12:49:32 oleg Exp $ */

/**
 * Specify language object. Language table.
 */
class language
{
    /**
     * int language id
     */
    var $id;

    /**
     * string language name
     */
    var $name;

    /**
     * string language short name
     */
    var $short_name;
}

/**
 * Get languages.
 *
 * @param $p_sort_field string sort field
 * @param $p_sort_order string sort order: ASC or DESC
 * @param $p_page int page number
 * @param $p_count int items count on the page
 * @param $p_filter class language_filter
 *
 * @return array languages array
 */
function get_languages($p_sort_field = 'id',
            $p_sort_order = 'ASC',
            $p_page = 1,
            $p_count = 0,
            $p_filter = NULL
            )
{
    $t_sort_field = db_escape_string($p_sort_field);
    $t_sort_order = db_prepare_sort_order($p_sort_order);
    $t_page = db_prepare_int($p_page);
    $t_count = db_prepare_int($p_count);

    if ($t_sort_field == '')
        $t_sort_field = 'id';

    if (!in_array($t_sort_field, array('id', 'name', 'short_name')))
    {
        $t_sort_field = 'id';
    }

    $order_str = "`$t_sort_field` $t_sort_order";

    if ($p_count != 0)
    {
        $limit = ($t_page-1)*$t_count;
        $limit_str = "$limit, $t_count";
    }
    else
    {
        $limit_str = '';
    }

    $tmp =  ($p_filter != NULL) ? $p_filter->query() : '';
    $languages = db_extract(DB_TABLE_LANGUAGES, $tmp,
            $order_str, $limit_str);

    return $languages;
}

/**
 * Get languages count by filter.
 *
 * @param $p_filter class language_filter
 *
 * @return int languages count
 */
function get_languages_count($p_filter)
{
    $tmp =  ($p_filter != NULL) ? $p_filter->query() : '';
    if ($tmp != '')
    {
        $tmp = ' WHERE '.$tmp;
    }


    $query = 'SELECT COUNT(*) as `_count_`
             FROM '.DB_TABLE_LANGUAGES.$tmp;
    $result = db_query($query);

    return isset($result[0]['_count_']) ? $result[0]['_count_'] : 0;
}

/**
 * Get languages from languages id array.
 *
 * @param $p_id_array int array languages id
 *
 * @return array languages array
 */
function get_languages_from_array($p_id_array)
{
    $tmp = '';
    $array = array_values($p_id_array);
    $size = sizeof($array);

    if ($size == 0)
        return NULL;

    for($i = 0; $i < $size - 1; $i++)
        $tmp .=  db_prepare_int($array[$i]).', ';
    $tmp .= db_prepare_int($array[$size-1]);

    $languages = db_extract(DB_TABLE_LANGUAGES, "id IN($tmp)", 'id ASC');

    return $languages;
}

/**
 * Delete language.
 *
 * @param $p_id int language id
 *
 * @return bool TRUE on success or FALSE on failure
 */
function delete_language($p_id)
{

    $t_id = db_prepare_int($p_id);

    //Delete translations.
    $query =  'DELETE FROM '.DB_TABLE_TRANSLATION.
          ' WHERE language_id='.$t_id;
    db_exec($query);

    if (db_last_error() == '')
    {
        $error = FALSE;
        $query =  'DELETE FROM '.DB_TABLE_LANGUAGES.' WHERE id='.$t_id;
        db_exec($query);
    }
    else
    {
        $error = TRUE;
    }


    return ((db_last_error() == '') && !$error);
}

/**
 * Delete languages.
 *
 * @param $p_id_array int array languages id array
 *
 * @return bool TRUE on success or FALSE on failure
 */
function delete_languages($p_id_array)
{
    $tmp = TRUE;
    foreach($p_id_array as $id)
    {
        $result =  delete_language($id);
        $tmp = $tmp && $result;
    }

    return $tmp;
}

/**
 * Add language.
 *
 * @param $p_name string language name
 * @param $p_short_name language short name
 * @param $p_copy bool copy english translations
 *
 * @return int language id
 */
function add_language($p_name, $p_short_name, $p_copy = TRUE)
{
    $t_name = db_prepare_string(trim($p_name));
    $t_short_name = db_prepare_string(trim($p_short_name));

    $query = 'INSERT INTO '.DB_TABLE_LANGUAGES.'(`name`, `short_name`)
        VALUES('.$t_name.', '.$t_short_name.')';

    $result = db_exec($query);
    $language_id = db_insert_id();
    if ($p_copy)
    {
        // Add translations.
        $translations = get_translations('id', 'ASC', 1, 0, 1);

        foreach($translations as $translation)
        {
            add_translation($language_id, $translation['name'],
                $translation['text']);
        }
    }
    return $language_id;
}

/**
 * Get language id by language name.
 *
 * @param $p_name string language name
 *
 * @return int language id on success or 0 on failure
 */
function get_language_id($p_name)
{
    $t_name = db_prepare_string(trim($p_name));

    $languages[] = db_extract(DB_TABLE_LANGUAGES, '`name` = '.$t_name);
    $id = 0;

    if (count($languages[0]) > 0)
    {
        $id = $languages[0][0]['id'];
    }

    return $id;
}

/**
 * Get language id by language short name.
 *
 * @param $p_short_name string language short name
 *
 * @return int language id on success or 0 on failure
 */
function get_language_id_by_short_name($p_short_name)
{
    $t_short_name = db_prepare_string(trim($p_short_name));

    $languages[] = db_extract(DB_TABLE_LANGUAGES,
                '`short_name` = '.$t_short_name);
    $id = 0;

    if (count($languages[0]) > 0)
    {
        $id = $languages[0][0]['id'];
    }

    return $id;
}

/**
 * Get language.
 *
 * @param $p_id int language id
 *
 * @return language class
 */
function get_language($p_id)
{
    $t_id = db_prepare_int($p_id);
    $languages[] = db_extract(DB_TABLE_LANGUAGES, '`id` = '.$t_id);
    $language = new language();
    if (count($languages[0]) > 0)
    {
        $language->id = $languages[0][0]['id'];
        $language->name = $languages[0][0]['name'];
        $language->short_name = $languages[0][0]['short_name'];
    }

    return $language;
}

/**
 * Edit language.
 *
 * @param $p_id int language id
 * @param $p_name string new language name
 * @param $p_short_name string new language short name
 *
 * @return adodb result
 */
function edit_language($p_id, $p_name, $p_short_name)
{
    $t_id = db_prepare_int($p_id);
    $t_name = db_prepare_string(trim($p_name));
    $t_short_name = db_prepare_string(trim($p_short_name));

    $query = 'UPDATE '.DB_TABLE_LANGUAGES.
         ' SET `name` = '.$t_name.',
         `short_name` = '.$t_short_name.
         ' WHERE `id` ='.$t_id;

    return db_exec($query);
}

/**
 * Update language.
 *
 * @param $p_name string language name
 * @param $p_short_name language short name
 *
 * @return int language id
 */
function update_language($p_name, $p_short_name)
{
    $id = get_language_id($p_name);

    if ($id == 0)
    {
        $id = add_language($p_name, $p_short_name, FALSE);
    }

    return $id;
}

